//
//  MSBaseCollectionPageView.m
//  MSJD
//
//  Created by xiangbo on 2018/12/14.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSBaseCollectionPageView.h"

@interface MSBaseCollectionShowCell : UICollectionViewCell

@property (nonatomic,strong)UIView *detailView;

@end

@implementation MSBaseCollectionShowCell
- (void)awakeFromNib{
    [super awakeFromNib];
}

- (void)setDetailView:(UIView *)detailView{
    _detailView = detailView;
    [self.contentView addSubview:_detailView];
    [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}
@end

@implementation MSBaseCollectionPageView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}

- (void)registetDetailView:(NSString *)className identifier:(NSString *)identifier{
    [super registetDetailView:className identifier:identifier];
    [self.collectionView registerClass:MSBaseCollectionShowCell.class forCellWithReuseIdentifier:identifier];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    id cellData = [self cellData:indexPath];
    if (!cellData) {
        if (self.dataArray.count > indexPath.row) {
            cellData = self.dataArray[indexPath.row];
        }
    }
    NSString *identifier = [self.delegate identifier:cellData indexPath:indexPath];
    NSString *className  =  self.identifierDic[identifier];
    if (!className) {
        identifier = NSStringFromClass(MSBaseCollectionShowCell.class);
    }
    MSBaseCollectionShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell.detailView) {
        Class detailClass = NSClassFromString(className);
        if (detailClass) {
            cell.detailView = [[detailClass alloc] init];
        }
    }
    
    if (self.isNeedConfigData) {
        if ([cell.detailView conformsToProtocol:@protocol(MSDetailViewDelegate)]) {
            
            [(id<MSDetailViewDelegate>)cell.detailView configData:cellData indexPath:indexPath];
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.item < self.dataArray.count) {
        MSFoodContentModel *contentModel = self.dataArray[indexPath.item];
        [MSJumpConn jumpToHomeFilterViewController:contentModel.kind.integerValue searchContent:contentModel.title isFromSearch:NO];
    }
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    return CGSizeZero;
//}

#pragma mark -Property

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
    }
    return _collectionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
